package com.xydzxt.www.controller.student;

import com.xydzxt.www.model.ExamRoom;
import com.xydzxt.www.model.Examinee;
import com.xydzxt.www.model.Major;
import com.xydzxt.www.model.Regist;
import com.xydzxt.www.service.ExamRoomService;
import com.xydzxt.www.service.ExamineeService;
import com.xydzxt.www.service.MajorService;
import com.xydzxt.www.service.RegistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class PrintController {
    @Autowired
    RegistService registService;
    @Autowired
    ExamineeService examineeService;
    @Autowired
    ExamRoomService examRoomService;
    @Autowired
    MajorService majorService;

    @GetMapping("/print")
    public String print(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();

        if (session.getAttribute("sfzh") != null) {
            String idNumber = session.getAttribute("sfzh").toString();
            Regist regist = registService.selectByIdNumber(idNumber);

            Examinee examinee = examineeService.selectByRegistId(regist.getRegistId());

            if (examinee != null) {
                if (examinee.getExamRoomId() != 0) {
                    ExamRoom examRoom = examRoomService.selectById(examinee.getExamRoomId());

                    Major major = majorService.selectById(regist.getFirstChoice());
                    req.setAttribute("regist", regist);
                    req.setAttribute("examinee", examinee);
                    req.setAttribute("examRoom", examRoom);
                    req.setAttribute("major", major);
                    return "student/printingZKZ";
                } else {
                    req.setAttribute("msg", "请等待教务进行排考！排考后可打印准考证！");
                    return "student/msg";
                }
            } else {
                req.setAttribute("msg", "请先进行报考缴费！计费完成后可打印准考证！");
                return "student/msg";
            }
        } else {
            resp.sendRedirect("index");
        }
        return "";
    }
}